package com.junyang.showdata.service.impl;

import com.junyang.showdata.common.*;
import com.junyang.showdata.mapper.CityDataMapper;
import com.junyang.showdata.pojo.CityCountWithName;
import com.junyang.showdata.pojo.CityData;
import com.junyang.showdata.service.CityDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * @Author fusijia
 * @Date 2025/1/14 9:10
 * @notes CityDataService实现类
 */
@Service
@Transactional
public class CityDataServiceImpl implements CityDataService {

    @Autowired
    private CityDataMapper cityDataMapper;

    /**
     *@Author：fusijia
     *@Date：2025/1/13 16:07
     *@return：可展示的数据列表
     *@param: 城市id
     *
     */
    @Override
    public List<CityData> getListById(int id){
       return cityDataMapper.getListById(id);
    }

    @Override
    public List<CityData> getCompleteList() {
        return cityDataMapper.getCompleteList();
    }

    @Override
    public List<CityData> getRiskByTime(Integer vTime) {
        List<CityData> RiskByTimeList;
        if (vTime == 0) {
            RiskByTimeList = cityDataMapper.RiskByYear();
        } else if (vTime == 1) {
            RiskByTimeList = cityDataMapper.RiskByMonth();
        } else{
            RiskByTimeList = cityDataMapper.RiskByweek();
        }
        return RiskByTimeList;
    }

    /**
     * @notes 根据风险等级查所有数据和完成数据
     *@Author：fusijia
     *@Date：2025/1/14 16:02
     *@return：返回该风险等级下的总数和完成数
     *@param: 风险等级
     */
    @Override
    public RiskDemo getRiskCount(int riskLevel){
        //获取该风险下的总数
        int total = cityDataMapper.getRiskCount(riskLevel);
        //获取该风险下完成的数量    完成为0
        int complete = cityDataMapper.getRiskCountByFinish(riskLevel,0);
        RiskDemo riskDemo = new RiskDemo();
        riskDemo.setTotal(total);
        riskDemo.setComplete(complete);
        return riskDemo;

    }

    /**
     * @notes 获取各等级风险数据及总数
     *@Author：fusijia
     *@Date：2025/1/14 16:02
     *@return：RiskLevelDemo
     *@param:
     */
    @Override
    public RiskLevelDemo getRiskLevelCount(){
        //获取数据总数
        int total = cityDataMapper.getRiskCount(-1);
        //获取一级风险数量
        int firstRiskCount = cityDataMapper.getRiskCount(0);
        //获取二级风险数量
        int secondRiskCount = cityDataMapper.getRiskCount(1);
        //获取三级风险数量
        int thirdRiskCount = cityDataMapper.getRiskCount(2);
        //获取四级风险数量2
        int fourthRiskCount = cityDataMapper.getRiskCount(3);
        RiskLevelDemo riskLevelDemo = new RiskLevelDemo();
        riskLevelDemo.setTotal(total);
        riskLevelDemo.setFirstRiskCount(firstRiskCount);
        riskLevelDemo.setSecondRiskCount(secondRiskCount);
        riskLevelDemo.setThirdRiskCount(thirdRiskCount);
        riskLevelDemo.setFourthRiskCount(fourthRiskCount);
        return riskLevelDemo;
    }

    /**
     * @notes 获取年，月，周，日时间段内的数据数量
     *@Author：fusijia
     *@Date：2025/1/14 16:02
     *@return：数据数量RiskDataByYearAndDay
     *@param:
     */
    public RiskDataByYearAndDay getByYearAndDay() {
        //获取当天时间戳
        LocalDateTime firstTimeOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        //获取当年第一天时间时间戳
        LocalDateTime firstDayOfYear = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear())), LocalTime.MIN);
        //获取当月第一天时间时间戳
        LocalDateTime firstDayOfMonth = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
        //获取当周第一天时间时间戳
        LocalDateTime firstDayOfWeek = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.previous(DayOfWeek.MONDAY))), LocalTime.MIN);
        //获取当日结束时间时间戳
        LocalDateTime lastTimeOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        int riskDataByDay = cityDataMapper.getRiskCountByTime(firstTimeOfDay,lastTimeOfDay);
        int riskDataByWeek = cityDataMapper.getRiskCountByTime(firstDayOfWeek,lastTimeOfDay);
        int riskDataByMonth = cityDataMapper.getRiskCountByTime(firstDayOfMonth,lastTimeOfDay);
        int riskDataByYear = cityDataMapper.getRiskCountByTime(firstDayOfYear,lastTimeOfDay);
        RiskDataByYearAndDay riskDataByYearAndDay = new RiskDataByYearAndDay();
        riskDataByYearAndDay.setRiskDataByYear(riskDataByYear);
        riskDataByYearAndDay.setRiskDataByMonth(riskDataByMonth);
        riskDataByYearAndDay.setRiskDataByWeek(riskDataByWeek);
        riskDataByYearAndDay.setRiskDataByDay(riskDataByDay);
        return riskDataByYearAndDay;

    }


    /**
     *@notes:根据不同地区等级获取数据数量
     *@Author：fusijia
     *@Date：2025/1/15 10:02
     *@return：CityDataCountDemo 各等级地区数量数据多少
     *@param:
     */
    public CityDataCountDemo getCityDataCountByLevel(){
        CityDataCountDemo cityDataCountDemo = new CityDataCountDemo();
        int cityDataRegionCount = cityDataMapper.getCityDataCountByLevel(1);
        int cityDataCountyCount = cityDataMapper.getCityDataCountByLevel(2);
        int cityDataPowerSupplyCount = cityDataMapper.getCityDataCountByLevel(3);
        cityDataCountDemo.setCityDataRegionCount(cityDataRegionCount);
        cityDataCountDemo.setCityDataCountyCount(cityDataCountyCount);
        cityDataCountDemo.setCityDataPowerSupplyCount(cityDataPowerSupplyCount);
        return cityDataCountDemo;
    }

    /**
     *@notes:根据地区等级联合查询数据数量并排序和返回对地区名称列表
     *@Author：fusijia
     *@Date：2025/1/15 11:02
     *@return：CityDataCountDemo 各等级地区数量数据及名称列表
     * @param
     */
    public List<CityCountWithName> getCityDataMapByLevel(int level){
        List cityDataCountyList = cityDataMapper.getCityDataMapByLevel(level);
        cityDataCountyList = cityDataCountyList.subList(0,5);
        return cityDataCountyList;
    }

}
